/*********************************************************************************
 *      Copyright:  (C) 2022 Zhang Changxing<ZhangChangxingVIP@163.com>
 *                  All rights reserved.
 *
 *       Filename:  platform_conf.c
 *    Description:  This file defines a structure to store the configuration information \
 *                  for connecting to the MQTT server and completes the platform selection and configuration.
 *                 
 *        Version:  1.0.0(05/1/2022)
 *         Author:  Zhang Changxing <ZhangChangxingVIP@163.com>
 *      ChangeLog:  1, Release initial version on "05/1/2022  01:33:25 AM"
 *                 
 ********************************************************************************/

#ifndef  _CONF_H_
#define  _CONF_H_

#define DEVID_LEN   16
#define TIME_LEN   32

typedef struct mqtt_connect_ctx_s
{
    char        host[128];
    int         port;
    char        client_id[128];
    char        username[64];
    char        passwd[128];
    int         keepalive;
    int         platform_flag;/*Used to determine which platform to choose*/
}mqtt_connect_ctx_t;

typedef struct pub_ctx_s
{
    char        pub_topic[128];
    char        pub_message[1024];/*Stores messages to be published*/
    int         pub_flag;/*Flag used to determine if there is data to publish*/   
    int         platform_flag;/*Used to determine which platform to choose*/
    int         connect_flag;
    int         pub_qos;
    int         sum;
}pub_ctx_t;

typedef struct sub_ctx_s
{
    char        sub_topic[128];
    char        sub_message[1024];/*Stores messages for subscriptions*/
    int         sub_flag;/*Used to determine whether messages are delivered to the subscribed topic*/ 
    int         platform_flag;/*Used to determine which platform to choose*/
    int         sub_qos;
}sub_ctx_t;

typedef struct log_ctx_s{
    int     loglevel;/*The level of logging*/
    char    logfile[128];/*Log File Path*/
    int     logsize;/*Log file capacity*/
}log_ctx_t;

/*---------------------------------------------------------------------------------------------------
 *   description:   Packet structure sent to the server
 *        member:   $devid:     An array used to store device names
 *                  $strtime:   An array used to store dates and times
 *                  $temper:    Used to store the obtained temperature value
 ---------------------------------------------------------------------------------------------------*/
typedef struct pack_info_s
{
    char            devid[DEVID_LEN];
    char            strtime[TIME_LEN];
    float           temper;
    volatile int    samp_falg;
}pack_info_t;


//Sets the context for the serial port properties
#define SERIAL_NAME 128
typedef struct ttyusb_ctx_s{
    int     fd;                         //Serial port file descriptor
    int     baudrate;                   //Baud rate
    int     databits;                   //Data bits
    char    parity;                     //The parity bit
    int     stopbits;                   //Stop bits
    char    serial_name[SERIAL_NAME];   //Name of serial port device
    int     msend_len;                  //Maximum sending length at a time
    int     timeout;                    //Timeout period for receiving messages
    struct  termios old_termios;        //Store the original properties of the serial port
}ttyusb_ctx_t;

typedef struct thread_ctx_s
{
    volatile float   thre_value;
    volatile int     tty_flag;
    char    phone_buf[32];
    char    target_number[32];
}thread_ctx_t;

typedef struct iotd_ctx_s
{
    mqtt_connect_ctx_t mqtt_connect_ctx;
    pub_ctx_t   pub_ctx;
    sub_ctx_t   sub_ctx;
    log_ctx_t   log_ctx;
    pack_info_t pack_info;
    ttyusb_ctx_t ttyusb_ctx;
    thread_ctx_t thread_ctx;
}iotd_ctx_t;

enum PLATFORM
{
    ALI, TENGCENT, HUAWEI
};


/*---------------------------------------------------------------------------------------------------
 *   description:   Initialize flat, log files, and serial port properties
 *          args:   $iotd_ctx:      Address of the context structure
 *                  $paltform:      Id of the cloud platform
 *  return value:      <0: failure,    =0:ok 
 ---------------------------------------------------------------------------------------------------*/
int inifile_parsing(iotd_ctx_t *iotd_ctx, int platform);


/*---------------------------------------------------------------------------------------------------
 *   description:   Re-select the cloud platform that reports the attributes
 *          args:   $iotd_ctx:      Address of the context structure
 *                  $paltform:      Id of the cloud platform
 *  return value:      <0: failure,    =0:ok 
 ---------------------------------------------------------------------------------------------------*/
int choose_platform(iotd_ctx_t *iotd_ctx, int platform);
#endif